package com.dmai.aiet.archive.controller;


import com.dmai.aiet.archive.entity.SysUser;
import com.dmai.aiet.archive.service.ISysUserService;
import com.dmai.aiet.common.base.PageResult;
import com.dmai.aiet.common.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
public class SysUserController {
    @Autowired
    private ISysUserService userService;

//    @GetMapping(value = "/users")
//    public Mono<Result<List<SysUser>>> getAllUserInfo() {
//        Flux<SysUser> userInfo = userService.getAllUserInfo();
//        return userInfo.collectList().map(user -> Result.success(user));
//    }

    /**
     * 用户查询
     *
     * @param params
     * @return
     */
    @GetMapping("/users")
    public Mono<PageResult<SysUser>> findUsers(@RequestParam Map<String, Object> params) {
        return userService.getPageUserInfo(params);
    }

    @GetMapping(value = "/users/{id}")
    public Mono<Result<SysUser>> getUserById(@PathVariable("id") Long id) {
        Mono<SysUser> user = userService.getUserById(id);
        Mono<Result<SysUser>> userMono = user.map(Result::success);
        return userMono;
    }

    @PostMapping(value = "/users")
    public Mono<Result> saveUser(@RequestBody SysUser user) {
        Mono<SysUser> userMono = userService.saveUser(user);
        return userMono.thenReturn(Result.success());
    }

    @PutMapping(value = "/users/{id}")
    public Mono<Result> updateUser(@PathVariable(value = "id") Long id, @RequestBody SysUser user) {
        Mono<SysUser> userMono = userService.updateUser(id, user);
        return userMono.thenReturn(Result.success());
    }

    @DeleteMapping(value = "/users/{id}")
    public Mono<Result> deleteUser(@PathVariable("id") Long id) {
        Mono<Void> userMono = userService.deleteUser(id);
        return userMono.thenReturn(Result.success());
    }
}
